package cn.tianyigps.tianyigpsmiletask.customer;

import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.customer.Customer;
import cn.tianyigps.tianyigpsmiletask.assign.CalMile;
import cn.tianyigps.tianyigpsmiletask.assign.MileageCalculation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created with IDEA
 *
 * @description: 消费者
 * @author: cui.guibin
 * @create: 2019/3/19 20:55
 **/
@Component
@Slf4j
public class MileCustomer extends Customer {
    @Autowired
    CalMile calMile;


    @Override
    public boolean checkTask(Task task) {
        if (!CollectionUtils.isEmpty(task.getParams())) {
            //task的TaskType不能为空
            if (StringUtils.isEmpty(task.getTaskType())) {
                return false;
            }
            //newxtTask不能为空
            if (task.getNextTask() == null) {
                return false;
            }
            Task nextTask = task.getNextTask();
            //nextTask的TaskType不能为空
            if (StringUtils.isEmpty(nextTask.getTaskType())) {
                return false;
            }
            //nextTask的callback不能为空
            if (!nextTask.getParams().containsKey("callback") || StringUtils.isEmpty(nextTask.getParams().get("callback"))) {
                return false;
            }
            //id不能为空
            if (StringUtils.isEmpty(task.getId())) {
                return false;
            }

            Map<String, Object> equipmentIdsMap = task.getParams();
            if (equipmentIdsMap.containsKey("equipmentIds") && equipmentIdsMap.get("equipmentIds") != null) {
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public void consume(Task task) {
        calMile.calDistance(task);
    }
}
